package com.yanqu.road.dao.impl.wingroom;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.wingroom.workshop.UserWorkShopData;
import com.yanqu.road.entity.wingroom.workshop.WorkShopBuyCount;
import com.yanqu.road.entity.wingroom.workshop.WorkShopPool;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;

/**
 * @author ：zfr
 * @date ：2022/3/22 18:00
 */
public class UserWorkShopDataDaoImpl extends TempDataObjectDao<UserWorkShopData> {

    public static String insertSql = "INSERT INTO t_u_workshop(userId,workShopPool,buyCountMap,userRefreshTime,refreshTimes,timesRefreshTime) values(?,?,?,?,?,?)";
    @Override
    protected boolean addData(UserWorkShopData data) {
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONObject.toJSONString(data.getWorkShopPool())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONObject.toJSONString(data.getBuyCountMap())));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserRefreshTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getRefreshTimes()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getTimesRefreshTime()));
        if (getDb().executeNoneQuery(insertSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateSql = "UPDATE t_u_workshop set workShopPool=?,buyCountMap=?,userRefreshTime=?,refreshTimes=?," +
            "timesRefreshTime=? where userId=?";
    @Override
    protected boolean updateData(UserWorkShopData data) {
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, JSONObject.toJSONString(data.getWorkShopPool())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONObject.toJSONString(data.getBuyCountMap())));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserRefreshTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getRefreshTimes()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getTimesRefreshTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        if (getDb().executeNoneQuery(updateSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected UserWorkShopData parseData(ResultSet rs) throws SQLException {
        UserWorkShopData data = new UserWorkShopData();
        data.setUserId(rs.getLong("userId"));
        data.setWorkShopPool(JSONObject.parseObject(rs.getString("workShopPool"), new TypeReference<WorkShopPool>() {
        }));
        data.setBuyCountMap(JSONObject.parseObject(rs.getString("buyCountMap"), new TypeReference<Map<Integer, WorkShopBuyCount>>() {
        }));
        data.setUserRefreshTime(rs.getLong("userRefreshTime"));
        data.setRefreshTimes(rs.getInt("refreshTimes"));
        data.setTimesRefreshTime(rs.getLong("timesRefreshTime"));
        return data;
    }

    public UserWorkShopData getUserWorkShopData(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        UserWorkShopData data = null;
        String sql = "select * from t_u_workshop where userId = ?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            call.setLong(1, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                data = parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return data;
    }
}
